package com.ruochen.recipe.ui.fragment.personal;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aries.ui.view.radius.RadiusTextView;
import com.blankj.utilcode.util.SizeUtils;
import com.ruochen.common.adapter.RecyclerCommonAdapter;
import com.ruochen.common.adapter.RecyclerViewHolder;
import com.ruochen.common.base.BaseMvpFragment;
import com.ruochen.common.widget.RadiusConstraintLayout;
import com.ruochen.recipe.R;
import com.ruochen.recipe.customer.SpacesItemDecoration;
import com.ruochen.recipe.databinding.FragmentPersonalBinding;
import com.ruochen.recipe.entity.PersonalActionBean;
import com.ruochen.recipe.mvp.personal.PersonalCovenant;
import com.ruochen.recipe.mvp.personal.PersonalPresenter;

import java.util.Arrays;

/**
 * @Description 个人中心
 * @Auth RuoChen
 * @Date 13/03/2021, 20:49
 **/
public class PersonalFragment extends BaseMvpFragment<FragmentPersonalBinding, PersonalPresenter> implements PersonalCovenant.MvpView {

    private int currentPosition;
    private RecyclerCommonAdapter<PersonalActionBean> actionAdapter;

    private AccountInformationFragment accountInformationFragment;
    private AccountSecurityFragment accountSecurityFragment;
    private BindCookbookFragment bindCookbookFragment;
    private MessagePushSettingFragment messagePushSettingFragment;
    private CookbookShowSettingFragment cookbookShowSettingFragment;
    private AboutFragment aboutFragment;
    private Fragment currentFragment;
    @Override
    protected void initView(Bundle savedInstanceState) {
        viewBinding.rvActionList.setLayoutManager(new LinearLayoutManager(mContext));
        viewBinding.rvActionList.addItemDecoration(new SpacesItemDecoration() {
            @Override
            public int getSpaceBottom(int position, int itemCount) {
                return SizeUtils.dp2px(1);
            }
        });
        viewBinding.rvActionList.setAdapter(actionAdapter = new RecyclerCommonAdapter<PersonalActionBean>(mContext,
                R.layout.item_personal_action,
                Arrays.asList(
                        new PersonalActionBean("账户资料", "请填写账户资料", true),
                        new PersonalActionBean("账户安全", "修改密码", true),
                        new PersonalActionBean("绑定菜谱", "默认菜谱设置", true),
                        new PersonalActionBean("消息推送设置", "", true),
                        new PersonalActionBean("菜谱显示设置", "", true),
                        new PersonalActionBean("关于我们", "", true),
                        new PersonalActionBean("清除本地缓存", "154.93M")
                        )) {
            @Override
            protected void convert(RecyclerViewHolder holder, PersonalActionBean item, int position) {
                RadiusConstraintLayout rclItemContainer = holder.getView(R.id.rclItemContainer);
                RadiusTextView tvItemActionName = holder.getView(R.id.tvItemActionName);
                RadiusTextView tvItemActionNext = holder.getView(R.id.tvItemActionNext);
                rclItemContainer.getDelegate().setSelected(currentPosition == position);
                rclItemContainer.getDelegate().init();
                tvItemActionName.getDelegate().setSelected(currentPosition == position);
                tvItemActionName.getDelegate().init();
                tvItemActionNext.getDelegate().setSelected(currentPosition == position);
                tvItemActionNext.getDelegate().init();

                tvItemActionName.setText(item.getName());
                tvItemActionNext.setText(item.getRightTip());
            }

        });
        actionAdapter.setOnItemClickListener(new RecyclerCommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(actionAdapter.getData().get(position).isHasPager()){
                    if(currentPosition == position){
                        return;
                    }
                    currentPosition = position;
                    actionAdapter.notifyDataSetChanged();
                    if(position == 0){
                        if(accountInformationFragment == null){
                            accountInformationFragment = new AccountInformationFragment();
                        }
                        showActionFragment(accountInformationFragment);
                    }else if(position == 1){
                        if(accountSecurityFragment == null){
                            accountSecurityFragment = new AccountSecurityFragment();
                        }
                        showActionFragment(accountSecurityFragment);
                    }else if(position == 2){
                        if(bindCookbookFragment == null){
                            bindCookbookFragment = new BindCookbookFragment();
                        }
                        showActionFragment(bindCookbookFragment);
                    }else if(position == 3){
                        if(messagePushSettingFragment == null){
                            messagePushSettingFragment = new MessagePushSettingFragment();
                        }
                        showActionFragment(messagePushSettingFragment);
                    }else if(position == 4){
                        if(cookbookShowSettingFragment == null){
                            cookbookShowSettingFragment = new CookbookShowSettingFragment();
                        }
                        showActionFragment(cookbookShowSettingFragment);
                    }else if(position == 5){
                        if(aboutFragment == null){
                            aboutFragment = new AboutFragment();
                        }
                        showActionFragment(aboutFragment);
                    }
                }
            }
        });

        accountInformationFragment = new AccountInformationFragment();

        showActionFragment(accountInformationFragment);

    }

    private void showActionFragment(Fragment fragment){
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        if(currentFragment != null){
            fragmentTransaction.hide(currentFragment);
        }
        if(!fragment.isAdded()){
            fragmentTransaction.add(R.id.flRightContainer, fragment);
        }
        currentFragment = fragment;
        fragmentTransaction.show(fragment).commitAllowingStateLoss();
    }

    @Override
    protected PersonalPresenter createPresenter() {
        return new PersonalPresenter(this);
    }


}
